
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

    //shuffle()是一个Java Collections类方法,其工作原理是随机置换指定列表元素
    public class Cards {
        public static final String[] suits={"♥","♠","♣","♦"};

        //4个花色 13张牌
        public List<Card> buyCard(){
            List<Card>cardList=new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                for (int j = 1; j <=13 ; j++) {
                    int rank=j;
                    String suit=suits[i];
                    Card card=new Card(rank,suit);
                    cardList.add(card);
                }
            }
            return cardList;
        }

        public void shuffle(List<Card> cardList){

            Random random=new Random();

            for (int i = cardList.size()-1; i >0; i--) {
                int randIndex=random.nextInt(i);
                swap(cardList,i,randIndex);
            }
        }

        private void swap(List<Card>cardList,int i,int j) {
            Card tmp = cardList.get(i);
            cardList.set(i, cardList.get(j));
            cardList.set(j, tmp);

        /* 上边代码相当于下面数组交换,但是不能这样写
        Card tmp=cardList[i];
        cardList[i]=cardList[j];
        cradList[j]=tmp;
         */
        }

        public void drawCard(List<Card> cardList){
            List<Card>hand1=new ArrayList<>();
            List<Card>hand2=new ArrayList<>();
            List<Card>hand3=new ArrayList<>();

            List <List<Card> >hands=new ArrayList<>();
            hands.add(hand1);
            hands.add(hand2);
            hands.add(hand3);

            for (int i = 0; i < 5; i++) {
                for (int j = 0; j <3 ; j++) {
                    Card card=cardList.remove(0);
                    //hands.get(j).add(card);
                    List<Card> tmpHand=hands.get(j);
                    tmpHand.add(card);

                }
            }
            System.out.println("第一个人的牌:"+hand1);
            System.out.println("第二个人的牌:"+hand2);
            System.out.println("第三个人的牌:"+hand3);
        }

    }

